#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "ftp_client.h"

struct Command
{
	const char* cmd;
	void (*fp)(void);
} Command[] = 
	{
		{"pwd",pwd_ftp_client},
		{"cd",cd_ftp_client},
		{"mkdir",mkdir_ftp_client},
		{"rmdir",rmdir_ftp_client},
		{"ls",ls_ftp_client},
		{"put",put_ftp_client},
		{"get",get_ftp_client},
		{"rm",rm_ftp_client},
		{"bye",bye_ftp_client},
	};

int main(int argc,const char* argv[])
{
	signal(SIGINT,sigint);
	
	if(2 == argc)
		login_ftp_client(argv[1],21);
	else if(3 == argc)
		login_ftp_client(argv[1],atoi(argv[2]));
	else
	{
		puts("Use:./ftp <ip> [port]");
		return 0;
	}

	char cmd[256] , cmdn = sizeof(Command)/sizeof(Command[0]);
	for(;;)
	{
		printf("ftp> ");
		scanf("%s",cmd);

		int i = 0;
		while(i < cmdn && strcmp(cmd,Command[i].cmd)) i++;

		if(i < cmdn)
			Command[i].fp();
		else if('!' == cmd[0])
			system(cmd+1);
		else
			puts("未定义的指令！");
	}
}
